//
//  BrandViewController.m
//  GameBoss
//
//  Created by 张艺 on 15/4/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "BrandViewController.h"
#import "AFNetworking.h"
#import "Sections.h"
#import "BrandCell.h"
#import "Brands.h"
#import "ProductViewController.h"
#import "MJRefresh.h"
@interface BrandViewController ()
@property (nonatomic, retain) NSMutableArray *secArr;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, copy) NSString *type;
@end

@implementation BrandViewController
-(void)dealloc
{
    [_secArr release];
    [_collectionView release];
    [_type release];
    [_idStrUrl release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.secArr = [NSMutableArray array];
        self.title = @"品牌列表";
    }
    return self;
}
- (void)dataPath
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/brand/%@" , self.idStrUrl] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *partitionDic = responseObject[@"partition"];
        self.type = responseObject[@"type"];
        NSMutableDictionary *totalBrandsDic = [partitionDic objectForKey:@"totalBrands"];
        NSMutableArray *sectionsArr = [totalBrandsDic objectForKey:@"sections"];
        for (int i = 0 ; i < sectionsArr.count; i++) {
            Sections *sections = [[Sections alloc] init];
            NSMutableDictionary *sectionsDic = sectionsArr[i];
            [sections setValuesForKeysWithDictionary:sectionsDic];
            sections.brands = [NSMutableArray array];
            for (int i = 0; i < [sectionsDic[@"brands"] count]; i ++) {
                NSMutableDictionary *brandsDic =  sectionsDic[@"brands"][i];
                Brands *brands = [[Brands alloc] init];
                [brands setValuesForKeysWithDictionary:brandsDic];
                brands.brandsId = brandsDic[@"id"];
                brands.letter = brandsDic[@"letter"];
                brands.name = brandsDic[@"name"];
                [sections.brands addObject:brands];
                [brands release];
            }
             [self.secArr addObject:sections];
            [sections release];
        }
        [self.collectionView reloadData];
        // 结束刷新
        [self.collectionView headerEndRefreshing];
        [self creatCollectionView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (void)viewDidLoad {
    [self dataPath];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#pragma mark 设置背景图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    imageView.image = [UIImage imageNamed:@"table"];
    [self.view addSubview:imageView];
    [imageView release];

}
- (void)addHeaderNew
{
    __unsafe_unretained typeof(self)vc = self;
    [self.collectionView addHeaderWithCallback:^{
            [vc.collectionView reloadData];
    }];
    [self.collectionView headerBeginRefreshing];
}

#pragma mark 创建
- (void)creatCollectionView
{
      UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //瀑布流的高度
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width*3/7, 40);
    flowLayout.minimumInteritemSpacing = 1;
    //列间距
    flowLayout.minimumLineSpacing = 1;
    flowLayout.headerReferenceSize = CGSizeMake(0, 5);
    flowLayout.footerReferenceSize = CGSizeMake(0, 5);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 0, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[BrandCell class] forCellWithReuseIdentifier:@"reuse"];
    [flowLayout release];
    
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    Sections *sec = self.secArr[section];
    return sec.brands.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.secArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BrandCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    Sections *sec = self.secArr[indexPath.section];
    Brands *brands = sec.brands[indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.6 alpha:0.4];
    cell.letterLabel.text = brands.letter;
    cell.nameLabel.text = brands.name;
    cell.letterLabel.textColor = [UIColor whiteColor];
    cell.nameLabel.textColor = [UIColor whiteColor];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProductViewController *productVC = [[ProductViewController alloc] init];
    Sections *sec = self.secArr[indexPath.section];
    Brands *brands = sec.brands[indexPath.row];
    productVC.typeId = self.type;
    productVC.numID = brands.brandsId;
    [self.navigationController pushViewController:productVC animated:YES];
    [productVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
